<!--
 * @Author: tkp
 * @Date: 2022-04-24 16:47:18
-->
<template>
  <div class="select_class">
    <el-select v-bind="$attrs" :size="size" v-on="$listeners" :placeholder="placeholder" :multiple="multiple" :clearable="clearable" @change="selectChange">
      <el-option v-for="item in option" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled">
      </el-option>
    </el-select>
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: 'Select',
  props: {
    data: {
      type: Array,
      required: true
    },
    jsonType: {
      type: Object,
    },
    size: {
      type: String,
      default: 'medium'
    },
    multiple: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: '请选择'
    },
    clearable: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    data(val, oldVal) {
      this.formatOption(val)
    }
  },
  data() {
    return {
      selectVal: '',
      option: []
    }
  },
  mounted() {
    this.formatOption(this.data)
  },
  methods: {
    formatOption(data) {
      this.option = []
      for (let x = 0; x < data.length; x++) {
        let item = {
          label: data[x][this.jsonType.label],
          value: data[x][this.jsonType.value],
          disabled: data[x][this.jsonType.disabled] || false
        }
        this.option.push(item)
      }
    },
    selectChange(e) {
      this.$emit('select', e)
    }
  }
}
</script>

<style lang='scss' scoped>
.select_class {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
}
</style>